﻿using AngryManagementMayhem.Content.Textures.MainMenu;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace AngryManagementMayhem.GameCode.Screens.MainMenuScreen.MainMenuObjects
{
    public class IconButton : Container
    {
        private readonly ImageContainer _buttonLeft;
        private readonly ImageContainer _buttonRight;

        private bool _clicked;

        public IconButton(ImageContainer icon)
        {

            _clicked = false;
            const float buttonSize = 2;
            _buttonLeft = new ImageContainer
                {
                    Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.SpriteList.ButtonLeft)
                };

            _buttonRight = new ImageContainer
            {
                PositionGridBound = new TE_Vector2(buttonSize, 0),
                Sprite = MainMenuSpriteSheet.GetSprite(MainMenuSpriteSheet.SpriteList.ButtonRight),
                Origin = TE_Rectangle.Alignment.TopRight,
            };

            AddChildren(_buttonLeft, _buttonRight);

            icon.PositionGridBound = new TE_Vector2(1, 1);
            icon.Origin = TE_Rectangle.Alignment.CenterMiddle;
            AddChildren(icon);

            BoundingBox = new TE_Rectangle(0, 0, buttonSize, 2);

            HoverOutColor();
         }

         protected override void OnMouseEnter(TE_Vector2 mousePosition)
         {
             HoverInColor();
             base.OnMouseEnter(mousePosition);
         }

         protected override void OnMouseLeave(TE_Vector2 mousePosition)
         {
             HoverOutColor();
             base.OnMouseLeave(mousePosition);
         }

        public void Click()
        {
            _clicked = !_clicked;

            if (!_clicked) 
                HoverInColor();
            else
            {
                Color c = new Color(255, 131, 0);
                _buttonLeft.Color = c;
                _buttonRight.Color = c;
            }
            
        }

        void HoverOutColor()
        {
            if (_clicked) return;
            Color c = new Color(0, 74, 127);
            _buttonLeft.Color = c;
            _buttonRight.Color = c;
        }

        void HoverInColor()
        {
            if (_clicked) return;

            Color c = new Color(0, 148, 255);
            _buttonLeft.Color = c;
            _buttonRight.Color = c;
        }
    }
}